package com.company.dcm.server.tcp;

import io.netty.channel.Channel;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ConnectionManager {
    private static final Map<String, Channel> channels = new ConcurrentHashMap<>();
    public static Map<String,String> serialToIpPort = new ConcurrentHashMap<>();
    public static Map<String,String> ipPortToSerial = new ConcurrentHashMap<>();
    public static void addChannel(String id, Channel channel) {
        channels.put(id, channel);
    }

    public static Set<String> getAllSerials() {
        return serialToIpPort.keySet();
    }

    public static Channel getChannel(String id) {
        return channels.get(id);
    }

    public static void removeChannel(String id) {
        channels.remove(id);
    }

    // 获取所有的 IP 和端口
    public static Set<String> getAllIpPorts() {
        Set<String> allIpPorts = new HashSet<>();
        allIpPorts.addAll(serialToIpPort.values());  // 添加 serialToIpPort 中的所有 IP 和端口
        allIpPorts.addAll(ipPortToSerial.keySet());  // 添加 ipPortToSerial 中的所有 IP 和端口
        return allIpPorts;
    }

    // 获取 serialToIpPort 中的所有 IP 和端口
    public static Set<String> getAllIpPort() {
        return (Set<String>) serialToIpPort.values();
    }

    // 获取 ipPortToSerial 中的所有 IP 和端口
    public static Set<String> getAllIpPortFromReverse() {
        return ipPortToSerial.keySet();
    }
}
